package cn.xiaye.blog.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.xiaye.blog.bean.Keyword;
import cn.xiaye.blog.db.util.DbUtil;

public class KeywordDao {
	DbUtil dbUtil = DbUtil.getDefaultDatabaseUtil();

	Keyword keyword = new Keyword();

	public void insertKeyword() {
		PreparedStatement ps = null;
		ps = dbUtil.prepareStatement("insert into keword values(?,?)");
		try {
			ps.setString(1, keyword.getName());
			ps.setInt(2, keyword.getArticleId());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbUtil.close(ps);
		}

	}

	public void deleteKeyword() {
		PreparedStatement ps = null;
		ps = dbUtil.prepareStatement("");
		try {
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			dbUtil.close(ps);
		}
	}

	public void updateKeyword() {
		PreparedStatement ps = null;
		ps = dbUtil.prepareStatement("");
		try {
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			dbUtil.close(ps);
		}
	}

	public List<Keyword> queryAll() {
		PreparedStatement ps = null;
		Keyword keyword = new Keyword();
		List<Keyword> keyList = new ArrayList<Keyword>();
		ps = dbUtil.prepareStatement("select * from keyword");
		ResultSet rs = null;
		try {
			rs = ps.executeQuery();
			while (rs.next()) {
				keyword.setId(rs.getInt("id"));
				keyword.setName(rs.getString("name"));
				keyword.setArticleId(rs.getInt("article_id"));
				keyList.add(keyword);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbUtil.close(ps);
			dbUtil.close(rs);
		}
		return keyList;
	}

}
